package org.wms.minbearwcs.dao.stock.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.wms.minbearwcs.dao.stock.inf.MaterialStockDao;
import org.wms.minbearwcs.entity.Mapper.stock.MaterialStockMapper;
import org.wms.minbearwcs.entity.PO.wcs.stock.MaterialStock;

import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class MaterialStockDaoImpl implements MaterialStockDao {

    private final MaterialStockMapper mapper;

    @Override
    public boolean insert(MaterialStock materialStock) {
        return mapper.insert(materialStock) == 1;
    }

    @Override
    public boolean updateBatchCodeByPallet(String pallet, String batchCode) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(MaterialStock::getPalletCode,pallet)
                .set(MaterialStock::getBatchCode,batchCode)
                .update();
    }

    @Override
    public String selectMaterialCodeByPallet(String palletCode) {
        Optional<MaterialStock> optional = new LambdaQueryChainWrapper<>(mapper)
                .eq(MaterialStock::getPalletCode, palletCode)
                .select(MaterialStock::getMaterialCode)
                .last("limit 1")
                .oneOpt();

        return optional.map(MaterialStock::getMaterialCode).orElse(null);
    }

    @Override
    public String selectProductionLineByPallet(String palletCode) {
        Optional<MaterialStock> optional = new LambdaQueryChainWrapper<>(mapper)
                .eq(MaterialStock::getPalletCode, palletCode)
                .select(MaterialStock::getProductionLine)
                .last("limit 1")
                .oneOpt();

        return optional.map(MaterialStock::getProductionLine).orElse(null);
    }
}
